package com.huayue.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.BaseDao;
import com.huayue.dao.PurchaseOrderDao;
import com.huayue.dao.PurchaseOrderDetailsDao;
import com.huayue.entity.PurchaseOrder;
import com.huayue.entity.PurchaseOrderDetails;

/**
 * 请购单
 * 
 */
@Service
public class PurchaseOrderService extends BaseService<PurchaseOrder> {

    @Autowired
    PurchaseOrderDao dao;

    @Autowired
    PurchaseOrderDetailsDao detailsDao;

    @Override
    public BaseDao<PurchaseOrder> getDao() {
        return dao;
    }

    /**
     * 查找最大id
     * 
     * @return
     */
    public int findMaxId() {

        int i = 0;
        try {
            i = dao.findMaxId();
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    /**
     * 添加订单信息
     * 
     * @param orderInfo
     * @param details
     * @return
     */
    public boolean addDetails(PurchaseOrder orderInfo, List<PurchaseOrderDetails> details) {
        int success = 0;
        // 计算总件数和总金额
        int buyCount = 0;
        BigDecimal price = new BigDecimal("0");
        for (PurchaseOrderDetails orderDetails : details) {
            buyCount += orderDetails.getNumber();
            price = price.add(orderDetails.getPrice());
        }
        // 完善订单信息
        orderInfo.setBuycount(buyCount);
        orderInfo.setPrice(price.add(orderInfo.getOtherprice()));
        // 将订单信息添加到数据库
        success = dao.insert(orderInfo);
        // 判断是否添加成功
        if (success == 1) {
            // 将订单详情添加到数据库
            for (PurchaseOrderDetails orderDetails : details) {
                success += detailsDao.insert(orderDetails);
            }
        }
        if (success == (details.size() + 1)) {
            return true;
        }
        return false;
    }

    /**
     * 查询订单详情
     * 
     * @param id
     * @return
     */
    public List<Map<String, Object>> findDetails(String id) {
        List<Map<String, Object>> details = detailsDao.findDetails(id);
        List<Map<String, Object>> finalDetails = new ArrayList<Map<String, Object>>();
        // 处理向前台发送数据
        for (Map<String, Object> map : details) {
            Map<String, Object> newMap = new HashMap<String, Object>();
            for (int i = 0; i < map.size(); i++) {
                newMap.put("productId", map.get("product_id"));
                newMap.put("supplierId", map.get("supplier_id"));
                newMap.put("product_number", map.get("product_number"));
                newMap.put("number", map.get("number"));
                newMap.put("unit", map.get("unit"));
                newMap.put("onePrice", map.get("price"));
                newMap.put("price",
                    Double.valueOf(map.get("price").toString()) * Double.valueOf(map.get("number").toString()));
                newMap.put("cause", map.get("cause"));
            }
            finalDetails.add(newMap);
        }

        return finalDetails;
    }

    /**
     * 修改订单状态
     * 
     * @param purchaseOrder
     * @return
     */
    public int check(PurchaseOrder purchaseOrder) {
        return dao.changeStatus(purchaseOrder);
    }
}
